package ink.lovejinhu.swardToOffer;

import java.util.HashMap;

/**
 * @Author: jin
 * @Date: 2021/8/31 18:19
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Offer_039 {
    /**
     * 数组中出现次数超过了一半
     *
     * @param nums
     * @return
     */
    public int majorityElement(int[] nums) {
        HashMap<Integer, Integer> res = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (res.containsKey(nums[i])) {
                int temp = res.get(nums[i]) + 1;
                if (temp * 2 > nums.length) {
                    return nums[i];
                }
                res.put(nums[i], temp);
            } else {
                res.put(nums[i], 1);
            }
        }
        return nums[0];
    }
}
